import { defHttp } from '@/utils/http/axios';
import { AxiosPromise } from 'axios';
import { ProductVO, ProductForm, ProductQuery } from '@/api/product/types';

/**
 * 查询产品信息列表
 * @param query
 * @returns {*}
 */

export const listProduct = (query?: ProductQuery): AxiosPromise<ProductVO[]> => {
  return defHttp.get({
    url: '/product/spu/list',
    params: query,
  });
};

/**
 * 查询产品信息详细
 * @param ProductId
 */
export const getProduct = (ProductId: string): AxiosPromise<ProductVO> => {
  return defHttp.get({
    url: '/product/spu/' + ProductId,
  });
};

/**
 * 新增产品信息
 * @param data
 */
export const addProduct = (data: ProductForm) => {
  return defHttp.post({
    url: '/product/spu',
    data: data,
  });
};

/**
 * 修改产品信息
 * @param data
 */
export const updateProductFllow = (
  ProductId: string | number,
  followIds: string | number | undefined,
) => {
  return defHttp.put({
    url: '/product/spu/' + ProductId + '/' + followIds,
  });
};

/**
 * 删除产品信息
 * @param ProductId
 */
export const delProduct = (ProductId: string | number | Array<string | number>) => {
  return defHttp.delete({
    url: '/product/spu/' + ProductId,
  });
};

/**
 * 关注、取消关注产品
 * @param data
 */
export const updateProduct = (data: ProductForm) => {
  return defHttp.put({
    url: '/product/spu',
    data: data,
  });
};

export function exportProduct() {
  // 一定要加 responseType: 'blob'
  // 一定要加 responseType: 'blob'
  // 一定要加 responseType: 'blob'
  // 否则拿到的是string
  return defHttp.post<Blob>(
    { url: '/product/spu/export', responseType: 'blob' },
    { isTransformResponse: false },
  );
}

/**
 * 新增产品规格
 * @param data
 */
export const addProductSku = (data: any) => {
  return defHttp.post({
    url: '/product/sku',
    data: data,
  });
};

/**
 * 修改产品地址
 * @param data
 */
export const updateProductAddress = (data: any) => {
  return defHttp.put({
    url: '/product/address',
    data: data,
  });
};

/**
 * 获取产品规格
 * @param query
 */
export const getProductSku = (spuId: any) => {
  return defHttp.get({
    url: '/product/sku/' + spuId,
  });
};

/**
 * 删除产品地址
 * @param addressIds
 */
export const delProductAddress = (addressIds: Array<string | number>) => {
  return defHttp.delete({
    url: '/product/address/' + addressIds,
  });
};

/**
 * 新增产品联系人
 * @param data
 */
export const addProductContact = (data: any) => {
  return defHttp.post({
    url: '/product/contact',
    data: data,
  });
};

/**
 * 修改产品联系人
 * @param data
 */
export const updateProductContact = (data: any) => {
  return defHttp.put({
    url: '/product/contact',
    data: data,
  });
};

/**
 * 查询产品联系人
 * @param ProductId
 */
export const getProductContact = (query?: ProductQuery): AxiosPromise<ProductVO[]> => {
  return defHttp.get({
    url: '/product/contact/list',
    params: query,
  });
};

/**
 * 删除产品联系人
 * @param contactIds
 */
export const delProductContact = (contactIds: Array<string | number>) => {
  return defHttp.delete({
    url: '/product/contact/' + contactIds,
  });
};
